#include "server.h"
#include "ui_server.h"
#include "mytcpserver.h"
#include <QFile>
#include <QtDebug>
#include <QMessageBox>
#include <QHostAddress>

Server::Server(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Server)
{
    ui->setupUi(this);
    //加载配置
    loadConfig();
    //监听
    MyTcpServer::getInstance().listen(QHostAddress(m_strIP),m_usPort);
}

Server::~Server()
{
    delete ui;
}

void Server::loadConfig()
{
    QFile file(":/server.config");//文件路径，固定冒号开头
    if(file.open(QIODevice::ReadOnly)) {//打开文件，只读
    QByteArray baData = file.readAll();

    QString strData = QString(baData);//存下来
    QStringList strList = strData.split("\r\n");//按\r\n拆分
    m_strIP = strList.at(0);//设置为类的成员变量
    m_usPort = strList.at(1).toShort();//设置为类的成员变量
    m_rootDir = strList.at(2);
    qDebug() << "loadConfig ip:"<< m_strIP <<"port:"<< m_usPort<<"m_rootDir"<<m_rootDir;//防止找不到是哪个函数的，在打印的时候加上名
    file.close();//关闭文件
    }
    else {
        QMessageBox::warning(this,"open config", "open config failed");//或者打开失败
    }
}

Server &Server::getInstance()
{
    static Server instance;
    return instance;
}
